@page "/post"

@using MyBlogServer.Models
@using MyBlogServer.Services
@inject PostService PostService
@inject IJSRuntime JSRuntime
@inject IMarkdownService MarkdownService

@rendermode InteractiveServer

<PageTitle>博客文章</PageTitle>

<div class="blog-container">
    <h1>博客文章</h1>

    <div class="blog-content-wrapper">
        <div class="blog-posts-container">
            @if (_posts == null || !_posts.Any())
            {
                <div class="alert alert-info">
                    <p>暂无文章</p>
                </div>
            }
            else
            {
                foreach (var post in _posts)
                {
                    <div class="blog-post">
                        <h2><a href="/post/@post.Slug">@post.Title</a></h2>
                        <div class="meta">
                            <span>发布时间: @post.CreatedAt.ToString("yyyy年MM月dd日")</span>
                        </div>
                        @if (!string.IsNullOrEmpty(post.Summary))
                        {
                            <div class="summary">
                                @post.Summary
                            </div>
                        }
                        else
                        {
                            <div class="summary">
                                @((MarkupString)MarkdownService.ToHtml(post.Content.Length > 200 ? post.Content.Substring(0, 200) + "..." : post.Content))
                            </div>
                        }
                        <div class="read-more">
                            <a href="/post/@post.Slug">阅读全文</a>
                        </div>
                        @if (!string.IsNullOrEmpty(post.Tags))
                        {
                            <div class="post-tags">
                                @foreach (var tag in post.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).Where(t => !string.IsNullOrEmpty(t)))
                                {
                                    <a href="/tag/@tag" class="tag">@tag</a>
                                }
                            </div>
                        }
                    </div>
                }
            }
        </div>
        
        <aside class="tag-cloud-sidebar">
            <h3>标签云</h3>
            @if (PostService.GetAllTags().Any())
            {
                @foreach (var tag in PostService.GetAllTags())
                {
                    <a href="/tag/@tag" class="tag">@tag</a>
                }
            }
            else
            {
                <p>暂无标签</p>
            }
        </aside>
    </div>
</div>

@code {
    private IEnumerable<Post> _posts = new List<Post>();

    protected override async Task OnInitializedAsync()
    {
        _posts = await PostService.GetAllPostsAsync();
    }
    
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            // 调用JavaScript函数为标签分配随机颜色
            await JSRuntime.InvokeVoidAsync("assignRandomTagColors");
        }
    }
}